<!--
Copyright 2013 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!DOCTYPE html><meta charset="UTF-8">
<body></body>
<script src="../bootstrap.js"></script>
<script>

var body = document.querySelector('body');
var anims = [body, body, body, body, body, body, body, body, body, body].map(function(element, idx) {
  return new Animation(element, { left: '100px' }, idx);
});

function initGroup() {
 return new AnimationGroup(anims.slice(0, 5));
}

function toOrdering(list) {
  return [].map.call(list, function(animation) { return animation.timing.duration; });
}

function assert_child_order(group, list, message) {
  assert_array_equals(toOrdering(group.children), list, message);
}

test(function() {
  var group = new AnimationGroup();
  group.append(anims[5]);
  assert_child_order(group, [5],
    'append on empty group should work');
  var group = initGroup();
  group.append(anims[5]);
  assert_child_order(group, [0, 1, 2, 3, 4, 5],
    'append should place element 5 at end of group');
  group.append(anims[6], anims[7], anims[8]);
  assert_child_order(group, [0, 1, 2, 3, 4, 5, 6, 7, 8],
    'append should place elements 6, 7, and 8 at end of group');
}, 'append');

test(function() {
  var group = new AnimationGroup();
  group.prepend(anims[5]);
  assert_child_order(group, [5],
    'prepend on empty group should work');
  var group = initGroup();
  group.prepend(anims[5]);
  assert_child_order(group, [5, 0, 1, 2, 3, 4],
    'prepend should place element 5 at beginning of group');
  group.prepend(anims[6], anims[7], anims[8]);
  assert_child_order(group, [6, 7, 8, 5, 0, 1, 2, 3, 4],
    'prepend should place elements 6, 7, and 8 at beginning of group');
}, 'prepend');

test(function() {
  var group = initGroup();
  assert_equals(group.firstChild, anims[0],
    'first child should be element 0');
  group.prepend(anims[8]);
  assert_equals(group.firstChild, anims[8],
    'first child after prepend should be prepended element');
}, 'firstChild');

test(function() {
  var group = initGroup();
  assert_equals(group.lastChild, anims[4],
    'last child should be element 4');
  group.append(anims[8]);
  assert_equals(group.lastChild, anims[8],
    'last child after append should be appended element');
}, 'lastChild');

test(function() {
  var group = initGroup();
  group.children[2].before(anims[5]);
  assert_child_order(group, [0, 1, 5, 2, 3, 4],
    'before should place element 5 before element 2');
  anims[3].before(anims[6], anims[7], anims[8]);
  assert_child_order(group, [0, 1, 5, 2, 6, 7, 8, 3, 4],
    'before should place elements 6, 7, and 8 before element 3');
  group.firstChild.before(anims[9]);
  assert_child_order(group, [9, 0, 1, 5, 2, 6, 7, 8, 3, 4],
    'before should place element 9 at beginning of list');
}, 'before');

test(function() {
  var group = initGroup();
  group.children[2].after(anims[5]);
  assert_child_order(group, [0, 1, 2, 5, 3, 4],
    'after should place element 5 after element 2');
  anims[3].after(anims[6], anims[7], anims[8]);
  assert_child_order(group, [0, 1, 2, 5, 3, 6, 7, 8, 4],
    'after should place elements 6, 7, and 8 after element 3');
  group.lastChild.after(anims[9]);
  assert_child_order(group, [0, 1, 2, 5, 3, 6, 7, 8, 4, 9],
    'after should place element 9 at end of list');
}, 'after');

test(function() {
  var group = initGroup();
  group.children[2].replace(anims[5]);
  assert_child_order(group, [0, 1, 5, 3, 4],
    'replace should replace element 2 with element 5');
  anims[3].replace(anims[6], anims[7], anims[8]);
  assert_child_order(group, [0, 1, 5, 6, 7, 8, 4],
    'replace should replace element 3 with elements 6, 7, and 8');
  group.firstChild.replace(anims[9]);
  assert_child_order(group, [9, 1, 5, 6, 7, 8, 4],
    'replace should replace element 0 with element 9');
  group.lastChild.replace(anims[0]);
  assert_child_order(group, [9, 1, 5, 6, 7, 8, 0],
    'replace should replace element 4 with element 0');
}, 'replace');

test(function() {
  var group = initGroup();
  group.children[2].remove();
  assert_child_order(group, [0, 1, 3, 4],
    'element 2 should be removed');
  group.firstChild.remove();
  assert_child_order(group, [1, 3, 4],
    'first child should be removed');
  group.lastChild.remove();
  assert_child_order(group, [1, 3],
    'last child should be removed');
}, 'remove');

test(function() {
  var group = initGroup();
  var group2 = new AnimationGroup();
  group2.append(group);
  var group3 = new AnimationSequence();
  group3.append(group);
  assert_throws("HierarchyRequestError",
    function() {
      group.append(group3);
    },
    'group3 should be in hierarchy of group');
  assert_throws("HierarchyRequestError",
    function() {
      group.append(group);
    },
    'group should be in its own hierarchy');
  assert_throws("HierarchyRequestError",
    function() {
      anims[3].replace(group);
    },
    'group should be in hierarchy of element 3');

}, 'inclusive ancestors fail');

</script>
